/**
 * @author KujoMiva
 * @name form.js
 * @desc 登录vuex
 * @time 2020年05月15日 14:42:04 星期五
 */
import { Message } from 'element-ui';
import store from '@/store';
// api
import { resetPassword } from '@/axios/userAccount';
import { registerAPI } from '@/api/account';

export default {
    namespaced: true,
    state: {
        nowAction: 'login',
        isVerifycodeLogin: false,
        // 关于这些属性, 外面有错误用法. 不应该让组件双向绑定state数据...  虽然很香
        sign: { value: '', tip: '', state: false, signType: null },
        nickname: { value: '', tip: '', state: false },
        password: { value: '', tip: '', state: false },
        verifyCode: { value: '', tip: '', state: false, sending: false, expiresIn: 0 },
    },
    mutations: {
        TOOGEL_NOW_ACTION: (state, data) => {
            state.nowAction = data;
        },
        TOOGEL_IS_VERIFYCODE_LOGIN: (state, data) => {
            state.isVerifycodeLogin = data;
        },
    },
    actions: {
        async loginHandler({ state }) {
            const { sign, password, verifyCode, isVerifycodeLogin } = state;
            const actionType = isVerifycodeLogin ? 'verifyCode' : 'password';
            const result = await store.dispatch('account/loginAction', {
                account: sign.value,
                password: password.value,
                verifyCode: verifyCode.value,
                actionType,
            });
            const { code, message } = result;
            const isSuccess = code === 200;
            // 不管结果如何, 给个提示
            Message({ message, type: isSuccess ? 'success' : 'error' });
            // 如果用户不存在
            if (code === 404) {
                // 这里就改变了state的属性了, 想个办法
                state.nowAction = 'register';
                state.verifyCode.value = '';
                throw new Error(message);
            }
            // 失败了抛出去一个错误, 之前就是这么干的. 先兼容一下
            if (!isSuccess) {
                throw new Error(message);
            }
        },

        async registerHandler({ commit, state }) {
            const { nickname, password, verifyCode, sign } = state;
            const params = {
                account: sign.value,
                nickname: nickname.value,
                password: password.value,
                verifyCode: verifyCode.value,
            };
            if (String(params.nickname).trim() === '') {
                nickname.tip = '昵称不能为空';
                nickname.state = false;
                return;
            }
            if (String(params.account).trim() === '') {
                sign.tip = '邮箱/手机号不能为空';
                sign.state = false;
                return;
            }
            if (String(params.password).trim() === '') {
                password.tip = '密码不能为空';
                password.state = false;
                return;
            }
            if (String(params.verifyCode).trim().length !== 6) {
                verifyCode.tip = '验证码格式不正确';
                verifyCode.state = false;
                return;
            }
            const { code, message } = await registerAPI(params);
            const status = code === 200;
            Message({ message, type: status ? 'success' : 'error' });
            if (status) {
                state.nowAction = 'login';
                state.verifyCode.value = '';
                commit('TOOGEL_IS_VERIFYCODE_LOGIN', false);
            }
        },

        async resetPassword({ commit, state }) {
            const { password, verifyCode, sign } = state;
            const { signType } = sign;
            const options = {
                password: password.value,
                verifyCode: verifyCode.value,
            };
            options[signType] = sign.value;
            if (String(options[signType]).trim() === '') {
                sign.tip = '邮箱/手机号不能为空';
                sign.state = false;
                return;
            }
            if (!signType) {
                sign.tip = '邮箱/手机号格式不正确';
                sign.state = false;
                return;
            }
            if (String(options.password).trim() === '') {
                password.tip = '密码不能为空';
                password.state = false;
                return;
            }
            if (String(options.verifyCode).trim().length !== 6) {
                verifyCode.tip = '验证码格式不正确';
                verifyCode.state = false;
                return;
            }
            const { code, message } = await resetPassword(options);
            const isSuccess = code === 200;
            Message({ message, type: isSuccess ? 'success' : 'error' });
            if (!isSuccess) {
                return;
            }
            state.nowAction = 'login';
            state.verifyCode.value = '';
            commit('TOOGEL_IS_VERIFYCODE_LOGIN', false);
        },
    },
};
